package com.cdx.dxmall.product.controller;

import com.cdx.dxmall.common.utils.PageUtils;
import com.cdx.dxmall.common.utils.R;
import com.cdx.dxmall.product.entity.AttrEntity;
import com.cdx.dxmall.product.entity.AttrGroupEntity;
import com.cdx.dxmall.product.service.AttrAttrgroupRelationService;
import com.cdx.dxmall.product.service.AttrGroupService;
import com.cdx.dxmall.product.service.AttrService;
import com.cdx.dxmall.product.service.CategoryService;
import com.cdx.dxmall.product.vo.AttrGroupRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author chedx
 * @email chedx@gmail.com
 * @date 2020-12-29 22:11:02
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;


    @PostMapping("/attr/relation")
    public R relationGroup(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }


    //attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vo) {
        attrService.deleteRelation(vo);
        return R.ok();
    }


    //product/attrgroup/{attrGroupId}/attr
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrRelation(@PathVariable("attrgroupId") String attrgroupId,
                          @RequestParam Map<String, Object> params
    ) {
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    //
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") String attrgroupId) {
        List<AttrEntity> attrEntity = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catelogId

    ){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long [] catlogpath = categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(catlogpath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
